<?php
class ModelTotalOrderDiscount extends Model {
    public function getTotal(&$total_data, &$total, &$taxes, $status = 0) {
        if ($this->config->get('order_discount_status') && $status) {    
            $this->load->language('total/order_discount');
            
            if($this->cart->getSubTotal() > 0){     //isset($total_data['shipping'])){
                if(isset($total_data['giftcard']) && $total_data['giftcard']['value'] > 0){
                    $temp_total = $total_data['subtotal']['value'] - $total_data['giftcard']['value'];
                }
                else{
                    $temp_total = $total_data['subtotal']['value']; // + $total_data['shipping']['value'];
                }
                $rate = $temp_total * -1 * ((1 * $this->config->get('order_discount_fee')) / 100);
                
                $total_data['order_discount'] = array( 
                    'title'      => $this->language->get('text_order_discount'),
                    'text'       => $this->currency->formatPrice($rate),
                    'value'      => $rate,
                    'sort_order' => $this->config->get('order_discount_sort_order'),
                    'total_code' => $this->config->get('order_discount_fee'),
                );
            } 
            
            $total += $rate;
            $total_data['total']['value'] += $rate;
            $total_data['total']['text'] = $this->currency->formatPrice($total_data['total']['value']);
            
            //$total_data['temp_total'] = $temp_total + $rate;
        }
    }
    
    public function getTotalDiscount(&$total_data, &$total, &$taxes) {
        if ($this->config->get('handling_status') && ($this->cart->getSubTotal() < $this->config->get('order_discount_total'))) {
            $this->load->language('total/order_discount');
            
            $total_data[] = array( 
                'title'      => $this->language->get('text_order_discount'),
                'text'       => $this->currency->format($this->config->get('order_discount_fee')),
                'value'      => $this->config->get('order_discount_fee'),
                'sort_order' => $this->config->get('order_discount_sort_order')
            );

            if ($this->config->get('order_discount_tax_class_id')) {
                if (!isset($taxes[$this->config->get('order_discount_tax_class_id')])) {
                    $taxes[$this->config->get('order_discount_tax_class_id')] = $this->config->get('order_discount_fee') / 100 * $this->tax->getRate($this->config->get('order_discount_tax_class_id'));
                } else {
                    $taxes[$this->config->get('order_discount_tax_class_id')] += $this->config->get('order_discount_fee') / 100 * $this->tax->getRate($this->config->get('order_discount_tax_class_id'));
                }
            }
            
            $total += $this->config->get('order_discount_fee');
            $total = max(0, $total);
        }
    }
}
?>